from django.shortcuts import render
from rest_framework import request
from rest_framework import status
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, GenericAPIView,RetrieveAPIView, CreateAPIView
from rest_framework.mixins import CreateModelMixin
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from headline.models import Columns, Headlines, HeadlineComments, OtherComments
from headline.serializers import ColumnsSerialier, HeadlineListSerialier, SendHeadlineSerializer, \
    HeadlineCommentsSerializer, OtherCommentsSerializer, OtherSubCommentSerializer, SubCommontSerializer, \
    GetHotHeadlineSerializer


# Create your views here.



class ColumnsListView(ListAPIView):
    """
    专栏分类视图
    """
    serializer_class = ColumnsSerialier
    queryset = Columns.objects.filter(is_active=True)

    # 排序
    filter_backends = [OrderingFilter]
    ordering_fields = ('weight',)


# 分页
class LargeResultsSetPagination(PageNumberPagination):
    page_size = 3
    page_size_query_param = 'page_size'
    max_page_size = 20


class HeadlineListlView(ListAPIView):
    """
    首页头条列表视图
    """
    serializer_class = HeadlineListSerialier
    pagination_class = LargeResultsSetPagination

    def get_queryset(self):
        column_id = self.kwargs['column_id']
        return Headlines.objects.filter(column=column_id, is_active=True).order_by('-create_time')

    # def get(self, request ,column_id):
    #     headline_lists = Headlines.objects.filter(column=column_id, is_active=True).order_by('-create_time')
    #     serializer = HeadlineListSerialier(headline_lists, many=True)
    #
    #     return Response(serializer.data)


class DetailView(RetrieveAPIView):
    """
    新闻详情视图
    """
    serializer_class = HeadlineListSerialier
    queryset = Headlines.objects.all()


class SendHeadlineView(CreateAPIView):
    """
    发送头条视图
    """
    # permission_classes = [IsAuthenticated]
    serializer_class = SendHeadlineSerializer


class HeadlineCommentView(ListAPIView):
    """
    头条详情评论显示视图
    """
    serializer_class = HeadlineCommentsSerializer
    queryset = HeadlineComments.objects.all()
    def get_queryset(self):
        headline_id = self.kwargs['headline_id']
        return self.queryset.filter(headline_id=headline_id).order_by('-create_time')


class SubCommentView(CreateAPIView):
    """
    头条详情评论提交视图
    """
    serializer_class = SubCommontSerializer


class OtherCommentsView(ListAPIView):
    """
    头条详情楼中楼评论显示视图
    """
    serializer_class =OtherCommentsSerializer
    queryset = OtherComments.objects.all()

    def get_queryset(self):
        headline_comment_id = self.kwargs['headline_comment_id']
        return self.queryset.filter(headline_comment_id=headline_comment_id)


class OtherSubCommentView(CreateAPIView):
    """
    楼中楼评论提交视图
    """
    serializer_class = OtherSubCommentSerializer


class GetHotHeadlineView(ListAPIView):
    serializer_class = GetHotHeadlineSerializer
    queryset = Headlines.objects.filter(is_active=True).order_by('-clicks')
    pagination_class = LargeResultsSetPagination

